<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Subject;
use App\Models\Subject as SubjectModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SubjectController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Subject(), function (Grid $grid) {
            $grid->simplePaginate();
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('user_id')->view('admin.fields.user_info');
            $grid->column('type')->using(SubjectModel::$EnumType);
            $grid->column('name');
            $grid->column('number');
            $grid->column('tel');
            $grid->column('img')->image(null, 50, 50);
            $grid->column('auth_type')->using(SubjectModel::$EnumAuthType);
            $grid->column('auth_img')->image(null, 50, 50);
            $grid->column('auth_pid');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->column('status')->using(SubjectModel::$EnumStatus);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('type')->select(SubjectModel::$EnumType);
                $filter->like('name');
                $filter->like('number');
                $filter->like('tel');
                $filter->equal('auth_type')->select(SubjectModel::$EnumAuthType);
                $filter->equal('status')->select(SubjectModel::$EnumStatus);

            });
            $grid->fixColumns(5, -2);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Subject(), function (Show $show) {
            $show->field('id');
            $show->field('auth_img');
            $show->field('auth_pid');
            $show->field('auth_type');
            $show->field('img');
            $show->field('name');
            $show->field('number');
            $show->field('status');
            $show->field('tel');
            $show->field('type');
            $show->field('user_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Subject(), function (Form $form) {
            $form->display('id');
            $form->text('auth_img');
            $form->text('auth_pid');
            $form->text('auth_type');
            $form->text('img');
            $form->text('name');
            $form->text('number');
            $form->text('status');
            $form->text('tel');
            $form->text('type');
            $form->text('user_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
